import { defineStore } from "pinia";
import { ref } from "vue";
import {userLoginApi,roleGetApi} from '../api/user.js'
import router from '../router';

export const useAccount = defineStore('account',()=> {
    //用户数据
    const userInfo = ref(null)
    const jwtToken = ref('')
    const permitData=ref(null)//定义数据
    //登录
    const loginAccount = async (payload) => {
        
       let res = await userLoginApi(payload)
       console.log(res);

       let {code,token,data} = res.data
       
       if(code===1){//登录成功
           userInfo.value=data
           jwtToken.value=token
           //使用角色id获取permission权限(修改后数据)
           let permit=await roleGetApi(data.roleId)
           console.log('permit',permit);
           permitData.value=permit.data.data[0]//要抛出
           
           router.push('/')
       }
    }
    return {
        userInfo,
        jwtToken,
        loginAccount,
        permitData
    }
},
{
    persist:{
        Key:'mvvm-account'
    }
})